//部队信息(生命值横条，士气值横条)

var troopInfo = {
	troopInfoContainerSprites : [],
	troopInfoSprites : [],
	troopMoraleSprites : [],
	
	_location : null,
	_size : null,
	
	Initialize : function(){
		this.troopInfoContainerSprites = [];
		this.troopInfoSprites = [];
		this.troopMoraleSprites = [];
		
		var width = tileEngine.tileWidth;
		var height = tileEngine.tileHeight;
		
		this._location = {x : 0, y : 0.375 * height};
		this._size = {width : 0.75 * width, height : 4};
	},
	
	_CreateInfoContainer : function(troopid, iniStrength){
		if (warTroop.troopHolders[troopid] == null)
			return;

		this._ClearTroopInfo(troopid);
		
		this.troopInfoContainerSprites[troopid] = this._CreateInfoContainerSprite(iniStrength);
		warTroop.troopHolders[troopid].addChild(this.troopInfoContainerSprites[troopid], 3);
	},
	
	_CreateInfoContainerSprite : function(iniStrength){
		var container = new cc.DrawNode();
		
		//draw morale divide point
		container.drawDot(
			cc.p(
				this._location.x +(-0.5 + campaign.judgement.surrend_morale) * this._size.width,
				this._location.y - 4),
			2,
			cc.color(223,0,0));
		
		//draw strength container
		var vSegNum = iniStrength / 10;
		for (var idx = 1; idx < vSegNum; idx++){
			this._DrawVSegment(
				container,
				this._location.x + (-0.5 + idx * 10 / iniStrength) * this._size.width,
				cc.color(0,0,0));
		}
		this._DrawVSegment(container, this._location.x - 0.5 * this._size.width - 2, cc.color(0,0,0));
		this._DrawVSegment(container, this._location.x + 0.5 * this._size.width + 2, cc.color(0,0,0));
		this._DrawHLine(container, this._location.y - 3, cc.color(63,63,63));
		this._DrawHLine(container, this._location.y + 3, cc.color(63,63,63));
		
		
		
		return container;
	},
	
	_DrawVSegment : function(drawNode, xpos, color){
		drawNode.drawSegment(
			cc.p(xpos, this._location.y - 3),
			cc.p(xpos, this._location.y + 3),
			1, color);
	},
	
	_DrawHLine : function(drawNode, ypos, color){
		drawNode.drawSegment(
			cc.p(this._location.x - 0.5 * this._size.width - 1, ypos),
			cc.p(this._location.x + 0.5 * this._size.width + 1, ypos),
			1, color);
	},
	
	_ClearInfoContainer : function(troopid){
		if (warTroop.troopHolders[troopid] == null){
			this.troopInfoContainerSprites[troopid] = null;
			return;
		}

		if (this.troopInfoContainerSprites[troopid] == null){
			return;
		}

		this.troopInfoContainerSprites[troopid].removeFromParent();
		this.troopInfoContainerSprites[troopid] = null;
	},
	
	RefreshTroopInfo : function(troopid, strengthPercent){
		if (warTroop.troopHolders[troopid] == null)
			return;
		
		if (strengthPercent < 0)
			strengthPercent = 0;
		
		this._ClearTroopInfo(troopid);
		
		this.troopInfoSprites[troopid] = this._CreateTroopInfoSprite(strengthPercent);
		warTroop.troopHolders[troopid].addChild(this.troopInfoSprites[troopid], 2);
	},
	
	_CreateTroopInfoSprite : function(strengthPercent){
		var troopInfo = new cc.DrawNode();
		troopInfo.drawSegment(
				cc.p(this._location.x - 0.5 * this._size.width, this._location.y),
				cc.p(this._location.x +(-0.5 + strengthPercent) * this._size.width, this._location.y),
				2,
				cc.color(0,223,0)
		);
		return troopInfo;
	},
	
	_ClearTroopInfo : function(troopid){
		if (warTroop.troopHolders[troopid] == null){
			this.troopInfoSprites[troopid] = null;
			return;
		}
		
		if (this.troopInfoSprites[troopid] == null){
			return;
		}
		
		this.troopInfoSprites[troopid].removeFromParent();
		this.troopInfoSprites[troopid] = null;
	},
	
	RefreshTroopMorale : function(troopid, morale){
		if (warTroop.troopHolders[troopid] == null)
			return;
		
		this._ClearTroopMorale(troopid);
		
		this.troopMoraleSprites[troopid] = this._CreateTroopMoraleSprite(morale);
		warTroop.troopHolders[troopid].addChild(this.troopMoraleSprites[troopid], 2);
	},
	
	_CreateTroopMoraleSprite : function(morale){
		var troopMorale = new cc.DrawNode();
		troopMorale.drawSegment(
				cc.p(this._location.x - 0.5 * this._size.width, this._location.y - 4),
				cc.p(this._location.x +(-0.5 + morale) * this._size.width, this._location.y - 4),
				2,
				cc.color(223,223,0)
		);
		return troopMorale;
	},
	
	_ClearTroopMorale : function(troopid){
		if (warTroop.troopHolders[troopid] == null){
			this.troopMoraleSprites[troopid] = null;
			return;
		}

		if (this.troopMoraleSprites[troopid] == null){
			return;
		}

		this.troopMoraleSprites[troopid].removeFromParent();
		this.troopMoraleSprites[troopid] = null;
	},

	CreateAllTroopInfo : function(){
		for (var troopid = 0; troopid < warTroop.troopHolders.length; troopid++){
			this._CreateInfoContainer(troopid, campaign_ori.troop[troopid].strength);
			this.RefreshTroopInfo(troopid, campaign.troop[troopid].strength / campaign_ori.troop[troopid].strength);
			this.RefreshTroopMorale(troopid, campaign.troop[troopid].morale);
		}
	},

	ClearAllTroopInfo : function(){
		for (var troopid = 0; troopid < warTroop.troopHolders.length; troopid++){
			this._ClearInfoContainer(troopid);
			this._ClearTroopInfo(troopid);
			this._ClearTroopMorale(troopid);
		}
	}
};